<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>onsubmit 表单提交事件</title>

    <script type="text/javascript">
        window.onload=function(){
            var form = document.getElementById("form");
            form.onsubmit = function (){
                //判断用户名.
                if(form.username.value.length <4 || form.username.value.length>6){
                    alert("username 4-6");
                    return false;
                }
                if(form.pwd.value.length !=6){
                    alert("pwd  is not 6");
                    return false;
                }
                if(form.pwd.value !=form.pwd2.value){
                    alert("两次pwd不一样");
                    return false;
                }

                //正则，然后 emialPatt.test 校验
                //用正则表达式 ^[\\w-]+@([a-zA-Z]+\\.)+[a-zA-Z}+$
                var eamilPatt =/^[\w-]+@([a-zA-Z]+\.)+[a-zA-Z]+$/;
                if(!eamilPatt.test(form.email.value)){
                    //提示用户电子邮件格式不正确.
                    alert("电子邮件不正确");
                    return  false;
                }
                return true;
            }
        }
    </script>


</head>
<body>
<h1>用户注册</h1>
<form action="ok.html" id="form">
    用户名:<input type="text" name="username">用户名4-6<br>
    密 码 :<input type="password" name="pwd">密码6<br>
    确 认 :<input type="password" name="pwd2">确认密码6<br>
    邮 箱 :<input type="text" name="email">邮箱<br>
    <input type="submit" value="注册用户">
</form>

</body>
</html>